<?php
session_start();
// 已登录则跳转到后台首页
if (isset($_SESSION['is_admin']) && $_SESSION['is_admin'] == 1) {
    header('Location: index.php');
    exit;
}

// 处理登录提交
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');
    $password = trim($_POST['password'] ?? '');
    
    // 简单验证（实际项目需从数据库查询）
    // 这里使用固定账号：admin / admin123（实际项目需替换为数据库验证）
    if ($username === 'test' && $password === '123456') {
        $_SESSION['is_admin'] = 1;
        $_SESSION['username'] = 'test';
        header('Location: index.php');
        exit;
    } else {
        $error = '用户名或密码错误';
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录 - IT知识库</title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
    <div class="login-container">
        <h1 class="login-title">管理员登录</h1>
        <form method="post">
            <div class="form-group">
                <label class="form-label" for="username">用户名</label>
                <input type="text" class="form-control" id="username" name="username" required>
            </div>
            <div class="form-group">
                <label class="form-label" for="password">密码</label>
                <input type="password" class="form-control" id="password" name="password" required>
            </div>
            <?php if ($error): ?>
                <div style="color: #ef4444; margin-bottom: 15px; font-size: 14px;"><?php echo $error; ?></div>
            <?php endif; ?>
            <button type="submit" class="btn btn-primary login-btn">登录</button>
        </form>
    </div>
</body>